﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Data;
using System.IO;
using System.Drawing;
public partial class administrator_uc_admin_a_banner : WebPageSecurity
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }   
    protected void lbtAddNewItem_Click(object sender, EventArgs e)
    {
        string strFileName = FileUploadBanner.FileName;
        if (!string.IsNullOrEmpty(strFileName))
        {
            try
            {
                Stream fStream = FileUploadBanner.FileContent;
                strFileName = FileUploadBanner.FileName.Replace(' ', '_');
                strFileName = strFileName.Substring(strFileName.LastIndexOf("\\") + 1);
                strFileName = CommonBLL.GetFilename(strFileName);

                string filepathsmallimage = Server.MapPath("" + DAL.EnvInfo.PathUpload + "smallimages//") + strFileName;
                string filepathlargeimage = Server.MapPath("" + DAL.EnvInfo.PathUpload + "images//") + strFileName;

                // Validate the uploaded file
                // 1. The uploaded file should not be a blank file
                if (FileUploadBanner.FileBytes.Length == 0)
                {
                    lblMessage.Text = "File bạn chọn bị lỗi! dung lượng file = " + FileUploadBanner.FileBytes.Length.ToString() + " bytes";
                    return;
                }
                // 2. The uploaded file should be having one of the allowed extensions
                string ext = System.IO.Path.GetExtension(FileUploadBanner.FileName).TrimStart(".".ToCharArray()).ToLower();
                if ((ext != "jpeg") && (ext != "jpg") && (ext != "png") && (ext != "gif") && (ext != "bmp") && (ext != "swf"))
                {
                    lblMessage.Text = "Định dạng file bạn chọn không hợp lệ (File hợp lệ dạng Ảnh hoặc Flash)";
                    return;
                }

                // Validation successful
                // Load the image into Bitmap Object

                //Bitmap uploadedImage = new Bitmap(FileUploadBanner.FileContent);

                // Set the maximum width and height here.
                // You can make this versatile by getting these values from
                // QueryString or textboxes
                //int maxWidth = 170;
                //int maxHeight = 160;

                // Resize the image
                //Bitmap resizedImage = CommonBLL.GetScaledPicture(uploadedImage, maxWidth, maxHeight);

                //Save the image
                // String virtualPath = "~/temp/" + System.Guid.NewGuid().ToString() + "." + ext;
                //String tempFileName = Server.MapPath(virtualPath);
                //resizedImage.Save(filepathsmallimage, uploadedImage.RawFormat);
                FileUploadBanner.SaveAs(filepathlargeimage);
                new CmsBLL().Banner_InsertItem(txtTitle.Text.Trim(), strFileName, rdFileType.SelectedValue + "", int.Parse("0" + rdActive.SelectedValue),DateTime.Now,DateTime.Now);
                lblMessage.Text = "Thêm mới thành công!";
                ResetForm();
                // Load the resized image on the browser
                //picture.ImageUrl = virtualPath;
                //imageContainer.Visible = true;
            }
            catch (Exception ex)
            {
                lblMessage.Text = "Eror! Message: " + ex.Message;
            }
        }
        else
        {
            lblMessage.Text = "Bạn chưa chọn file!";
            return;
        }        
    }
    private void ResetForm()
    {
        txtTitle.Text = "";
        rdFileType.SelectedValue = "Image";
        rdActive.SelectedValue = "1";
        txtTitle.Focus();
    }    
}